package com.security.door.controller.weixinManage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.security.door.utils.HttpRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Controller
@Api(value = "WEB - WeixinController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class WeixinController {

    private static String APP_SECRECT = "49b1e4ca59248226351366506350807d";
    private static String appId = "wx2f37beabca0fb06f";

    @GetMapping("/weixin/auth")
    @ApiOperation(value = "获取微信openID", notes = "获取微信openID")
    public void Auth(@RequestParam String code) {
        System.out.println(code);
        var OpenID = this.getOpenId(code, appId, APP_SECRECT);
        System.out.println(OpenID);
    }

    public String getOpenId(String code, String appId, String appSecret) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String json = "appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code";
        String response = HttpRequest.sendGet(url, json);
        JSONObject object = JSONObject.parseObject(response);
        String openId = object.getString("openid");
        return openId;
    }
}